home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 33 / Amiga Format AFCD33 (Issue 117, Dec 1998).iso / -seriously_amiga- / graphics / splitmpegppc / src / fileio.c < prev    next >
C/C++ Source or Header  |  1998-09-07  |  3KB  |  118 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  *
  21.  * I can be contacted via 
  22.  * Email: michael@ecel.uwa.edu.au
  23.  * Post: P.O. Box 506, NEDLANDS WA 6009, AUSTRALIA
  24.  *
  25.  * Amigaversion by Tobias Seiler in 1997
  26.  * Email: tabs@blader.com
  27.  */
  28.  
  29. #include "main.h"
  30. #include <string.h>
  31.  
  32. /*
  33.  *--------------------------------------------------------------
  34.  *
  35.  * get_more_data --
  36.  *
  37.  *    Called by correct_underflow in bit parsing utilities to
  38.  *      read in more data.
  39.  *
  40.  * Results:
  41.  *    Input buffer updated, buffer length updated.
  42.  *      Returns 1 if data read, 0 if EOF, -1 if error.
  43.  *
  44.  * Side effects:
  45.  *      None.
  46.  *
  47.  *--------------------------------------------------------------
  48.  */
  49.  
  50. int get_more_data(unsigned int *buf_start, int max_length, int *length_ptr, unsigned int **buf_ptr)
  51. {
  52.   
  53.   int length, num_read, request;
  54.   unsigned char *buffer, *mark;
  55.  
  56.   if (EOF_flag) return 0;
  57.  
  58.   length = *length_ptr;
  59.   buffer = (unsigned char *) *buf_ptr;
  60.  
  61.   if (length > 0) {
  62.     memcpy((unsigned char *) buf_start, buffer, (length*4));
  63.     mark = ((unsigned char *) (buf_start + length));
  64.   }
  65.   else {
  66.     mark = (unsigned char *) buf_start;
  67.     length = 0;
  68.   }
  69.  
  70.   request = (max_length-length)*4;
  71.   
  72.   num_read = fread( mark, 1, request, BitStream);
  73.  
  74.   /* Paulo Villegas - 26/1/1993: Correction for 4-byte alignment */
  75.   {
  76.     int num_read_rounded;
  77.     unsigned char *index;
  78.  
  79.     num_read_rounded = 4*(num_read/4);
  80.  
  81.     /* this can happen only if num_read<request; i.e. end of file reached */
  82.     if( num_read_rounded < num_read )
  83.       { 
  84.         num_read_rounded = 4*( num_read/4+1 );
  85.         /* fill in with zeros */
  86.         for( index=mark+num_read; index<mark+num_read_rounded; *(index++)=0 );
  87.         /* advance to the next 4-byte boundary */
  88.         num_read = num_read_rounded;
  89.       }
  90.   }
  91.  
  92.   if (num_read < 0) {
  93.     return -1;
  94.   }
  95.   else if (num_read == 0) {
  96.     *buf_ptr = buf_start;
  97.     
  98.     /* Make 32 bits after end equal to 0 and 32
  99.        bits after that equal to seq end code
  100.        in order to prevent messy data from infinite
  101.        recursion.
  102.     */
  103.  
  104.     *(buf_start + length) = 0x0;
  105.     *(buf_start + length+1) = ISO_11172_END_CODE;
  106.  
  107.     EOF_flag = 1;
  108.     return 0;
  109.   }
  110.  
  111.   num_read = num_read/4;
  112.  
  113.   *buf_ptr = buf_start;
  114.   *length_ptr = length + num_read;
  115.  
  116.   return 1;
  117. }
  118.